<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: cgl
 * @LastEditTime: 2023-06-15 17:36:11
 * @FilePath: \public-health-emergencies-fed:\radiation-hygiene-fe\src\views\basicData\calibrationFactor.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      @submit.native.prevent
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="80px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="queryParams.formDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              clearable
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="tableoperation">
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click.native.prevent="openDialog()"
        >添加</el-button
      >
    </div>

    <el-table
      v-loading="loading"
      :data="calibrationFactorList"
      :height="height - 400 + 'px'"
      @row-dblclick="(row) => openDialog(row, '3')"
    >
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column
        label="名称"
        align="center"
        prop="calibrationFactorName"
      />
      <el-table-column label="说明" align="center">
        <template slot-scope="scope">
          <el-tooltip
            :disabled="scope.row.remarks && scope.row.remarks.length <= 20"
            placement="top"
            :content="scope.row.remarks"
          >
            <span>{{ scope.row.remarks | ellipsis }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="nickName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button @click.native.prevent="openDialog(row)" type="text">
            编辑
          </el-button>
          <el-button
            @click.native.prevent="handleDelete(row)"
            type="text"
            style="color: #f32d2d"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="1000px"
      v-dialogDrag="true"
    >
      <el-form
        :model="form"
        :disabled="status == '3'"
        :rules="rules"
        ref="form"
        size="small"
        label-width="60px"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="名称" prop="calibrationFactorName">
              <el-input
                v-model="form.calibrationFactorName"
                placeholder="请输入"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="说明">
              <el-input
                type="textarea"
                rows="3"
                maxlength="100"
                show-word-limit
                v-model="form.remarks"
                :placeholder="status != '3' ? '请输入' : ''"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button type="primary" v-if="status != '3'" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  calibrationFactorList,
  calibrationFactorExport,
  calibrationFactorDetail,
  calibrationFactorAdd,
  calibrationFactorEdit,
  calibrationFactorDelete,
} from "@/api/basicData/calibrationFactor";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "calibrationFactor",
  dicts: ["equipment_state"],
  components: { Treeselect },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      dialogVisible: false,
      rules: {
        calibrationFactorName: [
          { required: true, message: "名称不能为空", trigger: "change" },
        ],
      },
      list: [
        { label: "1", value: "1" },
        { label: "2", value: "2" },
        { label: "3", value: "3" },
      ],
      // 视图高度
      height: document.documentElement.clientHeight,
      // 列表
      calibrationFactorList: [],
      // 表单参数
      form: {},
      loading: false,
      status: null,
      title: null,
    };
  },
  created() {
    this.getList();
  },
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 20) {
        return value.slice(0, 20) + "...";
      }
      return value;
    },
  },
  methods: {
    // 获取列表
    getList() {
      this.queryParams.createTimeStart =
        this.queryParams.formDate && this.queryParams.formDate[0];
      this.queryParams.createTimeEnd =
        this.queryParams.formDate && this.queryParams.formDate[1];
      let queryParams = { ...this.queryParams };
      delete queryParams.formDate;
      this.loading = true;
      calibrationFactorList(queryParams)
        .then((response) => {
          this.calibrationFactorList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 打开对话框
    openDialog(row, status) {
      console.log("子row", row);
      this.resetForm("form");
      if (row) {
        calibrationFactorDetail(row.calibrationFactorId).then((response) => {
          this.form = response.data;
          this.dialogVisible = true;
        });
        if (status == "3") {
          this.status = "3";
          this.title = "查看校准因子";
        } else {
          this.status = "2";
          this.title = "编辑校准因子";
        }
      } else {
        this.form = {};
        this.status = "1";
        this.title = "添加校准因子";
        this.dialogVisible = true;
      }
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) return;
        if (this.status == "1") {
          calibrationFactorAdd(this.form).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.dialogVisible = false;
            this.getList();
          });
        } else if (this.status == "2") {
          calibrationFactorEdit(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.getList();
          });
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除校准因子[${row.calibrationFactorName}]？`)
        .then(() => {
          this.loading = true;
          return calibrationFactorDelete(row.calibrationFactorId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea__inner {
  resize: none;
}
// ::v-deep .pagination-container {
// margin-top: 70px;
// }
</style>
